package JurisPro.modelo.banco;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import JurisPro.modelo.Evento;
import JurisPro.modelo.FilaEventos;
import java.sql.Timestamp;

public class EventoDAO {

	public static EventoDAO singleton = null;
	
	public static EventoDAO getInstance() {
		if(singleton == null)
			singleton = new EventoDAO();
		return EventoDAO.singleton;
	}
	
	public boolean insertEvento(Evento evento) {
		Timestamp dataSql = new Timestamp(evento.getData().getTime());

		String sql = "INSERT INTO Evento (numProcesso, IDUsuario, descricao, resultado, local, data) " +
				"VALUES (?, ?, ?, ?, ?, ?);";
		
		boolean error = false;
		
		PreparedStatement state = Conexao.getInstance().getPrepState(sql);
		try {
			state.setString(1, evento.getNumProcesso());
			state.setInt(2, evento.getResponsavel());
			state.setString(3, evento.getDescricao());
			state.setString(4, evento.getResultado());
			state.setString(5, evento.getLocal());
			state.setTimestamp(6, dataSql);
			error = !state.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return error;				
	}

	public int updateEvento(Evento evento) {
		Timestamp dataSql = new Timestamp(evento.getData().getTime());
		String sql = "UPDATE Evento SET IDUsuario = ?, descricao = ?, data = ?, local = ?, resultado = ? WHERE IDEvento = ? ;";
		
		PreparedStatement state = Conexao.getInstance().getPrepState(sql);
		int rows = 0;
		try {
			state.setInt(1, evento.getResponsavel());
			state.setString(2, evento.getDescricao());
			state.setTimestamp(3, dataSql);
			state.setString(4, evento.getLocal());
			state.setString(5, evento.getResultado());
			state.setInt(6, evento.getId());
			rows = state.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
		
	}

	public Evento selectEventoByID(int IDEvento) {
		String sql = "SELECT IDEvento, numProcesso, IDUsuario, convert(varchar, data, 103) as DATE, convert(varchar, data, 108) as HORA, data, descricao, local, resultado  FROM Evento where IDEvento = '"+ IDEvento+"';";
		Evento evento = null;
		ResultSet rs = Conexao.getInstance().selectComand(sql);
		try {
			while (rs.next()) {	
				Date data = rs.getTimestamp("data");
				evento = new Evento(rs.getInt("IDEvento"), rs.getString("numProcesso"), rs.getInt("IDUsuario"), data, 
						rs.getString("descricao"), rs.getString("local"), rs.getString("resultado"));				
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return evento;
	}

	public ArrayList<Evento> selectEventoOfProcesso(String numProcesso) {
		String sql = "SELECT IDEvento, numProcesso, IDUsuario, data, descricao, local, resultado FROM Evento where numProcesso = '"+ numProcesso+"';";
		Evento evento;
		ArrayList<Evento> eventos = new ArrayList<Evento>();
		ResultSet rs = Conexao.getInstance().selectComand(sql);
		try {
			while (rs.next()) {
				Date data = rs.getTimestamp("data");
				evento = new Evento(rs.getInt("IDEvento"), rs.getString("numProcesso"), rs.getInt("IDUsuario"), data, 
						rs.getString("descricao"), rs.getString("local"), rs.getString("resultado"));
				eventos.add(evento);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eventos;
	}
	
	public ArrayList<Evento> selectNextEventsOfProcesso(String numProcesso) {
		Date hoje = new Date();
		String sql = "SELECT IDEvento, numProcesso, IDUsuario, data, descricao, local, resultado FROM Evento where numProcesso = ? and data >= ?;";
		PreparedStatement state = Conexao.getInstance().getPrepState(sql);
		ResultSet rs = null;
		Evento evento;
		ArrayList<Evento> eventos = new ArrayList<Evento>();
		try {
			state.setString(1, numProcesso);
			state.setDate(2, new java.sql.Date(hoje.getTime()));		
			rs = state.executeQuery();
		
			while (rs.next()) {
				Date data = rs.getTimestamp("data");
				evento = new Evento(rs.getInt("IDEvento"), rs.getString("numProcesso"), rs.getInt("IDUsuario"), data, 
						rs.getString("descricao"), rs.getString("local"), rs.getString("resultado"));
				eventos.add(evento);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eventos;
	}
	
    public int deleteEvento(int IDEvento){
        String sql = "DELETE FROM Evento where IDEvento = "+IDEvento+"; ";
        return Conexao.getInstance().updateComand(sql);
}

	public ArrayList<FilaEventos> selectEventoByLawyer(int IDUsuario) {
		String sql = "SELECT e.IDEvento, p.pasta, p.numProcesso, p.autor, p.parte, p.cliente, " +
		"convert(varchar, e.data, 103) as DATE, e.data, e.descricao, e.local  FROM Evento AS e INNER JOIN Processo AS p "+ 
		"ON e.numProcesso = p.numProcesso WHERE (e.IDUsuario = "+ IDUsuario+") and ((e.data >= GETDATE()-1) and "+ 
		"(e.data < GETDATE()+7)) order by e.data;";
		FilaEventos evento = null;
		ArrayList<FilaEventos> eventos = new ArrayList<FilaEventos>();
		ResultSet rs = Conexao.getInstance().selectComand(sql);
		
		try {
			while (rs.next()) {	
				Date data = rs.getTimestamp("data");
				@SuppressWarnings("unused")
				String autor, parte;
				if(rs.getBoolean("cliente")){
					autor = rs.getString("autor")+" (Cliente)";
					parte = rs.getString("parte");
				} else{
					autor = rs.getString("autor");
					parte = rs.getString("parte")+" (Cliente)";					
				} 
			
                evento = new FilaEventos(rs.getInt("IDEvento"), rs.getInt("pasta"), rs.getString("numProcesso"), rs.getString("autor"), 
                rs.getString("parte"), data, rs.getString("descricao"), rs.getString("local"));
				eventos.add(evento);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eventos;
	}
	
	public ArrayList<FilaEventos> selectEventoByWeek() {
		String sql = "SELECT e.IDEvento, p.pasta, p.numProcesso, u.nome, p.autor, p.cliente, p.parte, " +
		"convert(varchar, e.data, 103) as DATE, e.data, e.descricao, e.local  FROM((" +
		"Evento AS e INNER JOIN Processo AS p ON e.numProcesso = p.numProcesso)INNER JOIN Usuario As u ON" +
		" p.IDResponsavel = u.IDUsuario) WHERE (e.data >= GETDATE()) and (e.data < GETDATE()+7) " +
		"GROUP BY e.IDEvento, p.pasta, p.numProcesso, u.nome, p.autor, p.cliente," +
		" p.parte, e.data, e.descricao, e.local ORDER BY e.data;";
		FilaEventos evento = null;
		ArrayList<FilaEventos> eventos = new ArrayList<FilaEventos>();
		ResultSet rs = Conexao.getInstance().selectComand(sql);
		
		try {
			while (rs.next()) {	
				Date data = rs.getTimestamp("data");
				String cliente=null;
				if(rs.getBoolean("cliente")){
					cliente = rs.getString("autor")+ " (Cliente)";					
				} else{					
					cliente = rs.getString("parte")+ " (Cliente)";					
				} 				
				evento = new FilaEventos(rs.getInt("IDEvento"), rs.getInt("pasta"), rs.getString("nome"), cliente, 
		                 data, rs.getString("descricao"), rs.getString("local") );
				eventos.add(evento);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eventos;
	}

	
}

